package cn.iocoder.yudao.module.famous.doctor.dal.mysql.performance;

import cn.hutool.core.util.IdUtil;
import cn.iocoder.yudao.module.famous.doctor.controller.app.operator.utils.FileUtil;
import cn.iocoder.yudao.module.famous.doctor.controller.app.operator.vo.OperatorPerformanceHead;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.form.UserProjectItemEntity;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.operator.OperatorPerformanceHeadMapper;
import cn.iocoder.yudao.module.famous.doctor.service.forimRecord.UserProjectItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sun 2020/10/22 16:24
 */
@Service
public class ManagerOperatorPerformanceService {

    @Transactional(rollbackFor = Exception.class)
    public OperatorPerformanceHead save(OperatorPerformanceHead operatorPerformanceHead) {
        operatorPerformanceHead.setImg(FileUtil.getShortFileName(operatorPerformanceHead.getImg()));
        if (operatorPerformanceHead.getId() == null) {
            operatorPerformanceHead.setIsTemplate(1);
            operatorPerformanceHead.setKey(IdUtil.fastSimpleUUID());
            operatorPerformanceHeadMapper.insert(operatorPerformanceHead);
        }
        else {
      /*      OperatorPerformanceHead old = operatorPerformanceHeadMapper.selectById(operatorPerformanceHead.getId());
            if (!old.getStatus().equals(operatorPerformanceHead.getStatus()))
            {
                List<OperatorPerformanceHead> operatorPerformanceHeads = operatorPerformanceHeadMapper.selectList(new QueryWrapper<OperatorPerformanceHead>().lambda()
                        .eq(OperatorPerformanceHead::getTemplateKey, operatorPerformanceHead.getKey()));
                for (OperatorPerformanceHead performanceHead : operatorPerformanceHeads) {
                    performanceHead.setStatus(operatorPerformanceHead.getStatus());
                    operatorPerformanceHeadMapper.updateById(performanceHead);
                }
            }*/
            operatorPerformanceHeadMapper.updateById(operatorPerformanceHead);

        }
        return operatorPerformanceHead;
    }

    @Transactional(rollbackFor = Exception.class)
    public void publishOperatorPerformance(Long id,int status)
    {
        OperatorPerformanceHead template = operatorPerformanceHeadMapper.selectById(id);
        template.setPublishStatus(status);
        operatorPerformanceHeadMapper.updateById(template);
        if (status == 1) {
//            List<UserOperator> userOperators = userOperatorMapper.selectList(new QueryWrapper<UserOperator>().lambda()
//                    .select(UserOperator::getId, UserOperator::getOperatorName));
//
//            List<UserProjectItemEntity> userProjectItemEntities = projectItemService.listByProjectKey(template.getKey());
//            for (UserOperator userOperator : userOperators) {
//                OperatorPerformanceHead operatorPerformanceHead = new OperatorPerformanceHead();
//                operatorPerformanceHead.setName(template.getName());
//                operatorPerformanceHead.setRemake(template.getRemake());
//                //默认停用
//                operatorPerformanceHead.setStatus(0);
//                operatorPerformanceHead.setImg(template.getImg());
////                operatorPerformanceHead.setOperatorName(userOperator.getOperatorName());
////                operatorPerformanceHead.setOperatorId(userOperator.getId());
//                operatorPerformanceHead.setKey(IdUtil.fastSimpleUUID());
//                operatorPerformanceHead.setTemplateKey(template.getKey());
//                operatorPerformanceHeadMapper.insert(operatorPerformanceHead);
//
//
//                for (UserProjectItemEntity userProjectItemEntity : userProjectItemEntities) {
//                    userProjectItemEntity.setId(null);
//                    userProjectItemEntity.setProjectKey(operatorPerformanceHead.getKey());
//                    userProjectItemEntity.setGmtCreate(null);
//                    userProjectItemEntity.setGmtModified(null);
//                }
//                projectItemService.saveBatch(userProjectItemEntities);
//            }
        }
        else
        {
            List<OperatorPerformanceHead> operatorPerformanceHeads = operatorPerformanceHeadMapper.selectList(new QueryWrapper<OperatorPerformanceHead>().lambda()
                    .eq(OperatorPerformanceHead::getTemplateKey, template.getKey()).eq(OperatorPerformanceHead::getStatus,0)
                    .select(OperatorPerformanceHead::getKey,OperatorPerformanceHead::getId));
            for (OperatorPerformanceHead operatorPerformanceHead : operatorPerformanceHeads) {
                projectItemService.remove(new QueryWrapper<UserProjectItemEntity>().lambda().eq(UserProjectItemEntity::getProjectKey,operatorPerformanceHead.getKey()));
            }
            List<Long> idList = operatorPerformanceHeads.stream().map(OperatorPerformanceHead::getId).collect(Collectors.toList());
            if(idList != null && idList.size() > 0) {
                operatorPerformanceHeadMapper.deleteBatchIds(idList);
            }
        }

    }

    @Autowired
    private OperatorPerformanceHeadMapper operatorPerformanceHeadMapper;
    @Autowired
    private UserProjectItemService projectItemService;
}
